package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalEntersCloudAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
@Service
@AttrHandler(LocalEntersCloudAttrHandler.class)
public class LocalEntersCloudAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteAttr cloudPoorAttr = inst.getAttr("210004834");
        if(Objects.nonNull(cloudPoorAttr)){
            String value = attr.getValue();
            if(KiteStringUtils.equals(value,"1_0000106")){
                cloudPoorAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                cloudPoorAttr.setIsNull(KeyConsts.IFTRUE_F);
                // after方法在这里改变为非必填,导致默认值逻辑错误设为空,这里还是补上
                if (KiteStringUtils.isEmpty(cloudPoorAttr.getValue())) {
                    operateInstAttrService.setAttrValue(inst, cloudPoorAttr.getFieldName(), "110008642", data.getSceneInstId(), null);
                }
            }else {
                cloudPoorAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                cloudPoorAttr.setIsNull(KeyConsts.IFTRUE_T);
            }
        }
        operateSceneInstService.setAttr(data.getSceneInstId(),inst.getInstType(),inst.getInstId(),cloudPoorAttr);
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteAttr cloudPoorAttr = inst.getAttr("210004833");
        String nodeCode = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "nodeCode");
        String serviceOfferId = data.getServiceOfferId();
        //签订、追单、重提、质检环节支持修改该业务属性
        if(KiteStringUtils.equalsAny(nodeCode,"OrderSigned","OrderResubmit","OrderAudit")||
            KiteStringUtils.equalsAny(serviceOfferId, LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW,"1",LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK)){
            cloudPoorAttr.setIsEdit(KeyConsts.IFTRUE_T);
        }else {
            cloudPoorAttr.setIsEdit(KeyConsts.IFTRUE_F);
        }
        operateSceneInstService.setAttr(data.getSceneInstId(),inst.getInstType(),inst.getInstId(),cloudPoorAttr);
    }
}
